<template>
  <el-container>
    <my-theme-card>
      <el-header>用户编辑</el-header>
      <el-main>

        <el-tabs v-model="currentTabPane" type="border-card">
          <el-tab-pane label="用户信息" name="1">
            <el-form ref="userForm" :model="tempUser" label-width="130px" :inline="true" :rules="userRules">
              <el-form-item label="账户名" prop="username">
                <el-input v-model="tempUser.username" />
              </el-form-item>
              <el-form-item label="别名" prop="nickname">
                <el-input v-model="tempUser.nickname" />
              </el-form-item>
              <el-form-item label="邮箱地址">
                <el-input v-model="tempUser.email" />
              </el-form-item>
              <el-form-item label="账户是否启用" prop="enabled">
                <el-switch
                  v-model="tempUser.enabled"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                />
              </el-form-item>
              <el-form-item label="账户是否锁定" prop="locked">
                <el-switch
                  v-model="tempUser.locked"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                />
              </el-form-item>
              <el-form-item label="账户是否过期" prop="expired">
                <el-switch
                  v-model="tempUser.expired"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="设置角色" name="2">
            <el-card class="box-card" shadow="hover">
              <div slot="header" class="clearfix">
                <span>角色列表</span>
              </div>
              <div class="grid-content bg-purple">
                <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="chooseRolesClick">新增</el-button>
                <template>
                  <el-table :data="roles" stripe border style="width: 100%">
                    <el-table-column prop="name" label="角色名称" width="180" />
                    <el-table-column prop="code" label="角色唯一编码" />
                    <el-table-column prop="description" label="角色描述" width="180" />
                    <el-table-column label="操作" width="180">
                      <template slot-scope="scope">
                        <el-button size="mini" @click="removeRole(scope.row)">取消</el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </template>
              </div>
            </el-card>
          </el-tab-pane>

          <el-row style="text-align: center;margin-top:20px;">
            <el-button type="primary" @click="editAllTask">确 定</el-button>
            <el-button @click="cancel">取消</el-button>
          </el-row>

        </el-tabs>

      </el-main>

      <el-dialog title="角色选择" :visible.sync="dialogRolesVisible" :close-on-click-modal="false" center append-to-body>
        <role-list @roleListClose="closeRolesDialog" />
      </el-dialog>
    </my-theme-card>
  </el-container>
</template>
<script>
import request from '@/utils/request'
const roleList = () => import('@/components/RoleList/index')
import { clone } from 'lodash'
export default {
  components: {
    roleList
  },
  props: {
    row: Object
  },
  data: function() {
    return {
      tempUser: {},
      roles: [],
      currentTabPane: '1',
      dialogRolesVisible: false,
      userRules: {
        username: [
          { required: true, message: '请输入用户账户', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        enabled: [
          { required: true, message: '请选择是否启用', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入别名', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {

  },
  created() {
    if (this.row) {
      this.tempUser.id = this.row.id
      this.tempUser.nickname = this.row.nickname
      this.tempUser.username = this.row.username
      this.tempUser.enabled = this.row.enabled
      this.tempUser.email = this.row.email
      this.tempUser = clone(this.tempUser)
    }
    this.queryUserRoles()
    this.directoryUpdate()
  },
  methods: {
    // 添加角色点击事件
    chooseRolesClick: function() {
      this.dialogRolesVisible = true
    },
    // 关闭角色选择框时
    closeRolesDialog: function(data, objs) {
      if (objs && objs.length > 0) {
        this.roles = objs
      }
      this.dialogRolesVisible = false
    },
    // 取消角色选择
    removeRole: function(row) {
      this.roles = this.roles.filter(item => {
        return item.id !== row.id
      })
    },
    // 字典更新
    directoryUpdate: function() {
      this.$store.commit('directory/setDirectory', 'logic')
    },
    // 取消
    cancel: function() {
      this.$emit('toChangePage', 'list')
    },
    // 验证所有表单
    validate: function() {
      const self = this
      return new Promise(function(resolve) {
        self.$refs['userForm'].validate((valid) => {
          if (valid) {
            resolve(true)
          } else {
            return false
          }
        })
      })
    },
    // 修改用户任务
    editAllTask: function() {
      // 验证表单内容后提交
      this.validate().then(valid => {
        if (valid) {
          this.$progress.start()
          request({
            url: '/system/userManager/editAll',
            method: 'post',
            data: {
              user: this.tempUser,
              roles: this.roles
            }
          }).then(res => {
            this.$progress.done()
            if (res.result) {
              this.cancel()
              this.$message.success(res.msg)
            } else {
              this.$message.error(res.msg)
            }
          }
          ).catch(() => { this.$progress.done() })
        }
      })
    },
    // 添加装备任务
    queryUserRoles: function() {
      request({
        url: '/system/userManager/queryUserRoles',
        method: 'post',
        data: this.tempUser
      }).then(res => {
        if (res.result) {
          this.roles = res.data
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { })
    }
  }
}
</script>
